package com.jx.demo.handler;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * 处理全局异常
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(ArithmeticException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Map<String,Object> handleException(Exception e) {
//        if (e instanceof ArithmeticException) {
//            return"params error";
//        }
//        if (e instanceof Exception) {
//            return"Internal server exception";
//        }
        Map<String,Object> error=new HashMap<>();
        error.put("code",400);
        error.put("msg","数字错误:"+e.getMessage());
        return error;
    }
}
